package com.xiangxiao.rpan.storage.controller;

import com.xiangxiao.rpan.storage.constant.ResponseCode;
import com.xiangxiao.rpan.storage.constant.ResponseMessage;
import com.xiangxiao.rpan.storage.dao.ReadmeConfigMapper;
import com.xiangxiao.rpan.storage.dto.JsonObject;
import com.xiangxiao.rpan.storage.entity.ReadmeConfig;
import com.xiangxiao.rpan.storage.service.ReadmeConfigService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/11/16 17:35
 */

@Slf4j
@RestController
@RequestMapping("/admin")
public class ReadmeConfigController {
  private static Logger logger = LoggerFactory.getLogger(ReadmeConfigController.class);
  @Resource
  private ReadmeConfigService readmeConfigService;

  @GetMapping("/storage/{storageId}/readme")
  public JsonObject getReadmeList(@PathVariable Integer storageId) {
    try {
      List<ReadmeConfig> configs = readmeConfigService.findByStorageId(storageId);
      return new JsonObject(ResponseMessage.QUERY_STORAGE_README_LIST_SUCCESS, ResponseCode.OK, configs);
    } catch (Exception e) {
      logger.error(ResponseMessage.QUERY_STORAGE_README_LIST_FAILED, e);
      return new JsonObject(ResponseMessage.QUERY_STORAGE_README_LIST_FAILED, ResponseCode.QUERY_STORAGE_README_LIST_FAILED);
    }
  }

  @PostMapping("/storage/{storageId}/readme")
  public JsonObject saveReadmeList(@PathVariable Integer storageId, @RequestBody List<ReadmeConfig> readme) {
    try {
      readmeConfigService.batchSave(storageId, readme);
      return new JsonObject(ResponseMessage.SAVE_STORAGE_README_LIST_SUCCESS, ResponseCode.OK, true);
    } catch (Exception e) {
      logger.error(ResponseMessage.SAVE_STORAGE_README_LIST_FAILED, e);
      return new JsonObject(ResponseMessage.SAVE_STORAGE_README_LIST_FAILED, ResponseCode.SAVE_STORAGE_README_LIST_FAILED);
    }
  }
}
